package org.jmresler.sakila.batch.utils;

/*
 * Converts the concatenated, upper case first and last names to
 * titled camel case so that customers don't think we're yelling
 * at them ;-)
 */
public class NameCaseConverter {

    public static String convertToTitleCaseIteratingChars(String text) {
        if (text == null || text.isEmpty()) {
            return text;
        }

        var converted = new StringBuilder();
        var convertNext = true;

        for (char ch : text.toCharArray()) {
            if (Character.isSpaceChar(ch)) {
                convertNext = true;
            } else if (convertNext) {
                ch = Character.toTitleCase(ch);
                convertNext = false;
            } else {
                ch = Character.toLowerCase(ch);
            }
            converted.append(ch);
        }

        return converted.toString();
    }
}
